package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {


    @Autowired
    private ProductCategoryMapper productCategoryMapper;



    //查询 三级分类
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
        /*PageHelper.startPage(pageNum, pageSize);
        List<ProductCategory> list = productCategoryMapper.selectAndOrderBy("0");
        List<ProductCategory> listAll = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> oneList = new ArrayList<>();
        for (ProductCategory category : list) {
            ProductCategoryTreeNodeVO oneVo = new ProductCategoryTreeNodeVO();
            //在其中设置二级分类
            List<ProductCategoryTreeNodeVO> twoList = new ArrayList<>();
            for (ProductCategory productCategory1 : listAll) {
                if (category.getId()==productCategory1.getPid()){
                    ProductCategoryTreeNodeVO twoVo = new ProductCategoryTreeNodeVO();
                    //设置三级分类
                    List<ProductCategoryTreeNodeVO> threeList = new ArrayList<>();
                    for (ProductCategory productCategory2 : listAll) {
                        if (productCategory1.getId()==productCategory2.getPid()){
                            ProductCategoryTreeNodeVO threeVo = new ProductCategoryTreeNodeVO();
                            BeanUtils.copyProperties(productCategory2,threeVo);
                            threeVo.setLev(3);
                            threeList.add(threeVo);
                        }
                    }
                    //拷贝
                    BeanUtils.copyProperties(productCategory1,twoVo);
                    twoVo.setLev(2);
                    //设置三级分类
                    twoVo.setChildren(threeList);
                    //添加至二级分类
                    twoList.add(twoVo);
                }
            }
            oneVo.setChildren(twoList);
            oneVo.setLev(1);
            oneList.add(oneVo);
            BeanUtils.copyProperties(category,oneVo);
        }
//      List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(oneList);
        PageInfo<ProductCategory> info = new PageInfo<>(list);
        return new PageVO<>(info.getTotal(), oneList);*/
        PageHelper.startPage(pageNum,pageSize);
        List<ProductCategory> productCategories = productCategoryMapper.selectAndOrderBy("0");
        CategoryTreeBuilder categoryTreeBuilder = new CategoryTreeBuilder();
        List<ProductCategory> categoryListAll = productCategoryMapper.selectAll();
        ProductCategoryConverter productCategoryConverter = new ProductCategoryConverter();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategoryConverter.converterToRoleVOList(categoryListAll);
        List<ProductCategoryTreeNodeVO> build = categoryTreeBuilder.build(productCategoryTreeNodeVOS);
        PageInfo<ProductCategory> info = new PageInfo<>(productCategories);

        return new PageVO<>(info.getTotal(),build);
    }
    //查询时添加父类
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        ProductCategoryConverter productCategoryConverter = new ProductCategoryConverter();
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategoryConverter.converterToRoleVOList(productCategories);
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
    }
    //物资类别添加
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }
    //回显
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);
        return productCategoryVO;
    }
    //修改
    @Override
    public Object update(Long id, ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setModifiedTime(new Date());
        return productCategoryMapper.updateByPrimaryKey(productCategory);
    }
    //删除
    @Override
    public Object delete(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);

        return productCategoryMapper.deleteByPrimaryKey(productCategory);
    }

    /*//查询ProductCategory数据库并转换成VO实现类
    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToRoleVOList(productCategories);
        return productCategoryTreeNodeVOS;
    }*/

}
